const req = require('../../utils/requests.js')
const tp_flip = require('../../modules/tp_flip/tp_flip.js')
const app = getApp()

Page({
  data: {
      datas: [],
      avatarAnim: {},
      album: {}
  },
  setAlbum:function(album) {
    if (album.title) {
      wx.setNavigationBarTitle({
        title: album.title
      })
    }
    var res = tp_flip.parseAnim(album.datas)
    
    setTimeout(function(){
      var dialogComponent = this.selectComponent('#loading')
      dialogComponent.hide();
      this.setData(res)

      wx.playBackgroundAudio({
        dataUrl: album.bg_music.url,
        title: '',
        coverImgUrl: ''
      })

      setTimeout(function(){
        // 音乐延迟10秒
        wx.stopBackgroundAudio()
      }, res.duration + 10000)
    }.bind(this), 100)

    var size = album.datas.length
    var tdatas = []
    for (var i = 0; i < size; i++) {
      tdatas.push({})
    }
    this.setData({
      datas: tdatas,
      album: album
    })
    
  },
  onLoad: function (options) {
    var dialogComponent = this.selectComponent('#loading')
    dialogComponent.hide();
    if (options && options.preview) {
      this.setAlbum(getApp().globalData.album)
      return
    }
    if (!(options && options.id)) {
      return
    }
    var albumId = options.id
    var that = this
    req.GET({
      action: '/wxapi/albums/' + albumId + '/',
      complete: function (code, msg, data) {
        that.setAlbum(data)
      }
    })
  },
  replay: function (e) {
    wx.redirectTo({
      url: '/pages/detail/detail?id=' + this.data.album.id,
    })
  },
  onShareAppMessage: function () {
    return {
      title: this.album.title ? this.album.title : '我制作了一个相册, 来看看吧',
      desc: '',
      path: '/pages/detail/detail?id=' + this.album.id + "&size=" + this.album.datas.length
    }
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.stopBackgroundAudio()
  },
})
